package persistence.impl;

import domain.Category;
import domain.Product;
import persistence.DBUtil;
import persistence.ProductDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class ProductDAOImpl implements ProductDao {
    private final String GET_PRODUCT_LIST_BYCATEGORY=
            "SELECT PRODUCTID,NAME,DESCN as description,CATEGORY as categoryId FROM PRODUCT WHERE CATEGORY = ?";
    private final String GET_PRODUCT=
            "SELECT PRODUCTID,NAME,DESCN as description,CATEGORY as categoryId FROM PRODUCT WHERE PRODUCTID = ?";
    private final String SEARCH_PRODUCT_LIST=
            "select PRODUCTID,NAME,DESCN as description,CATEGORY as categoryId from PRODUCT WHERE lower(name) like ?";
    @Override
    public List<Product> getProductListByCategory(String var1) {
        List<Product> products=new ArrayList<>();
        try{
            System.out.println(DBUtil.getConnection());
            Connection connection= DBUtil.getConnection();
            PreparedStatement preparedStatement=connection.prepareStatement(GET_PRODUCT_LIST_BYCATEGORY);
            preparedStatement.setString(1,var1);
            ResultSet resultSet=preparedStatement.executeQuery();
            while(resultSet.next())
            {
                Product product=new Product();
                product.setProductId(resultSet.getString("PRODUCTID"));
                product.setCategoryId(resultSet.getString("categoryId"));
                product.setName(resultSet.getString("NAME"));
                product.setDescription(resultSet.getString("description"));

                products.add(product);


            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closeStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e)
        {
            e.printStackTrace();
        }


        return products;
    }

    @Override
    public Product getProduct(String var1) {
        Product product=null;
        try{
            Connection connection= DBUtil.getConnection();
            PreparedStatement preparedStatement=connection.prepareStatement(GET_PRODUCT);
            preparedStatement.setString(1,var1);
            ResultSet resultSet=preparedStatement.executeQuery();
            if(resultSet.next())
            {
                product=new Product();
                product.setProductId(resultSet.getString("PRODUCTID"));
                product.setCategoryId(resultSet.getString("categoryId"));
                product.setName(resultSet.getString("NAME"));
                product.setDescription(resultSet.getString("description"));



            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closeStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e)
        {
            e.printStackTrace();
        }

        return product;
    }

    @Override
    public List<Product> searchProductList(String var1) {
        List<Product> productList=new ArrayList<>();
        try{
            Connection connection= DBUtil.getConnection();
            PreparedStatement preparedStatement=connection.prepareStatement(SEARCH_PRODUCT_LIST);
            preparedStatement.setString(1,var1);
            ResultSet resultSet=preparedStatement.executeQuery();
            while(resultSet.next())
            {
                Product product=new Product();
                product.setProductId(resultSet.getString("PRODUCTID"));
                product.setCategoryId(resultSet.getString("categoryId"));
                product.setName(resultSet.getString("NAME"));
                product.setDescription(resultSet.getString("description"));

                productList.add(product);


            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closeStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e)
        {
            e.printStackTrace();
        }

        return productList;
    }
}
